package remoteControl.panel;

import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ControlManualSensores extends Activity implements SensorEventListener 
{
 BluetoothAdapter btAdapter; 
 BluetoothSocket btSocket; 
 public BluetoothDevice device;
 private OutputStream out; 
 Button sendMsgBtn;
 String message;
 private SensorManager mgr;
 private Sensor accelerometer;
 private TextView text;
 private int mRotation;
	
 @Override
 public void onCreate(Bundle savedInstanceState) 
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.sensores);
  
  final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  final String btAdress = "00:06:66:07:AC:2A";
  btAdapter = BluetoothAdapter.getDefaultAdapter();
  device = btAdapter.getRemoteDevice(btAdress);
  
  sendMsgBtn = (Button) findViewById(R.id.button1);
  sendMsgBtn.setOnClickListener(stopListener);
  
  try 
  {
   btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
   btSocket.connect();
  } 
  catch (IOException e) 
  {
   Toast.makeText(getApplicationContext(), "Could not connect socket", Toast.LENGTH_LONG).show();
  }
  
  mgr = (SensorManager) this.getSystemService(SENSOR_SERVICE);
  accelerometer = mgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  
  text = (TextView) findViewById(R.id.textView1);
  
  WindowManager window = (WindowManager) this.getSystemService(WINDOW_SERVICE);
  int apiLevel = Integer.parseInt(Build.VERSION.SDK);
  
  if(apiLevel < 8) 
  {
   mRotation = window.getDefaultDisplay().getOrientation();
  }
  else 
  {
   mRotation = window.getDefaultDisplay().getRotation();
  }
  
  
  
 }  
  
  @Override
  protected void onResume() 
  {
   
	  mgr.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
	   super.onResume();
  }

  @Override
  protected void onPause() 
  {
   mgr.unregisterListener(this, accelerometer);
   super.onPause();
  }

  public void onAccuracyChanged(Sensor sensor, int accuracy) 
  { 
   // ignore 
  }

  public void onSensorChanged(SensorEvent event) 
  {
	

	  int operation = 4;
   String msg = String.format("X: %8.4f\nY: %8.4f\nZ: %8.4f\n",
   event.values[0], event.values[1], event.values[2], mRotation); 
   text.setText(msg);
   text.invalidate();
	 if(event.values[0] > 3)
	   {
		 
		 // RETROCEDER
		 mgr.unregisterListener(this, accelerometer);
		 operation = 7;
		   try 
		   {
			out = btSocket.getOutputStream();
		   } 
		   catch (IOException e) 
		   {
		    Toast.makeText(getApplicationContext(), "Could not listen data", Toast.LENGTH_LONG).show();
		   } 
		 try 
		   {
			   out.write(operation);
			   mgr.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
		   } 
		   catch (IOException e) 
		   {
		    Toast.makeText(getApplicationContext(), "Could not send data", Toast.LENGTH_LONG).show();
		   } 
	   }
	   else if(event.values[0] < -2)
	   {
		   // AVANZAR
		   mgr.unregisterListener(this, accelerometer);
		   operation = 1;
		   try 
		   {
			out = btSocket.getOutputStream();
		   } 
		   catch (IOException e) 
		   {
		    Toast.makeText(getApplicationContext(), "Could not listen data", Toast.LENGTH_LONG).show();
		   }
		   try 
		   {
			   out.write(operation);
			   mgr.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
		   } 
		   catch (IOException e) 
		   {
		    Toast.makeText(getApplicationContext(), "Could not send data", Toast.LENGTH_LONG).show();
		   } 
	   }
	   else if(event.values[1] > 3)
	   {
		   // DERECHA
		   mgr.unregisterListener(this, accelerometer);
		   operation = 5;
		   try 
		   {
			out = btSocket.getOutputStream();
		   } 
		   catch (IOException e) 
		   {
		    Toast.makeText(getApplicationContext(), "Could not listen data", Toast.LENGTH_LONG).show();
		   }
		   try 
		   {
			   out.write(operation);
			   mgr.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
		   } 
		   catch (IOException e) 
		   {
		    Toast.makeText(getApplicationContext(), "Could not send data", Toast.LENGTH_LONG).show();
		   } 
	   }
	   else if(event.values[1] < -3)
	   {
		   // IZQUIERDA
		   mgr.unregisterListener(this, accelerometer);
		   operation = 3;
		   try 
		   {
			out = btSocket.getOutputStream();
		   } 
		   catch (IOException e) 
		   {
		    Toast.makeText(getApplicationContext(), "Could not listen data", Toast.LENGTH_LONG).show();
		   }
		   try 
		   {
			   out.write(operation);
			   mgr.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
		   } 
		   catch (IOException e) 
		   {
		    Toast.makeText(getApplicationContext(), "Could not send data", Toast.LENGTH_LONG).show();
		   } 
	   }
	 }
  
  private Button.OnClickListener stopListener = new Button.OnClickListener() 
  {
   public void onClick(View v) 
   {
	   int operation = 4;
	   try 
	   {
		out = btSocket.getOutputStream();
	   } 
	   catch (IOException e) 
	   {
	    Toast.makeText(getApplicationContext(), "Could not listen data", Toast.LENGTH_LONG).show();
	   }
	   try 
	   {
		   out.write(operation);
	   } 
	   catch (IOException e) 
	   {
	    Toast.makeText(getApplicationContext(), "Could not send data", Toast.LENGTH_LONG).show();
	   }    
   }
   };
  
  @Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		try {
			btSocket.close();
			finish();
		} catch (IOException e) {
		}
	}

@Override
protected void onStop() {
	// TODO Auto-generated method stub
	super.onDestroy();
	try {
		btSocket.close();
		finish();
	} catch (IOException e) {
	}
}
  

 }
